import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;

import shop.local.valueobjects.Artikel;
import shop.local.valueobjects.ArtikelListe;
import shop.local.valueobjects.Ereignis;
import shop.local.valueobjects.Ereignisliste;
import shop.local.valueobjects.Kunde;
import shop.local.valueobjects.KundenListe;
import shop.local.valueobjects.MassengutArtikel;
import shop.local.valueobjects.Mitarbeiter;
import shop.local.valueobjects.MitarbeiterListe;
import shop.local.valueobjects.Warenkorb;

import java.io.*;
import java.net.*;

public class ShopClient {
	Socket requestSocket;
	ObjectOutputStream out;
 	ObjectInputStream in;
 	String message;
 	ShopClient(){
		try{

			requestSocket = new Socket("localhost", 2233);
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());

			try{
				message = (String)in.readObject();
				sendMessage("Hergestellt.");
			}
			catch(ClassNotFoundException classNot){
				System.err.println("Unbekanntes Datenformat.");
			}

		}
		catch(UnknownHostException unknownHost){
			System.err.println("Host nicht bekannt.");
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	void sendMessage(String msg)
	{
		try{
			out.writeObject(msg);
			out.flush();
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	
	public void schliessen() {
		try{
			in.close();
			out.close();
			requestSocket.close();
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}
	

	public ArtikelListe gibAlleArtikel() {
		try {
			out.writeObject("gibAlleArtikel");
			ArtikelListe liste = (ArtikelListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArtikelListe sucheNachTitel(String titel) {
		try {
			out.writeObject("sucheNachTitel");
			out.writeObject(titel);
			ArtikelListe liste = (ArtikelListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ArtikelListe sucheNachNummer(int artNr) {		
		try {
			out.writeObject("sucheNachNummer");
			out.writeObject(artNr);
			ArtikelListe liste = (ArtikelListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public MitarbeiterListe sucheNachMitarbeiterNummer(int mNr) {
		try {
			out.writeObject("sucheNachMitarbeiterNummer");
			out.writeObject(mNr);
			MitarbeiterListe liste = (MitarbeiterListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public KundenListe sucheNachKundenNummer(int kNr) {
		try {
			out.writeObject("sucheNachKundenNummer");
			out.writeObject(kNr);
			KundenListe liste = (KundenListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean legeArtikelAn(String titel, String beschreibung, int nummer, int bestand, float preis) {
		try {
			out.writeObject("legeArtikelAn");
			out.writeObject(titel);
			out.writeObject(beschreibung);
			out.writeObject(nummer);
			out.writeObject(bestand);
			out.writeObject(preis);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;		
	}

	public boolean legeArtikelAn(String titel, String beschreibung, int nummer, int bestand, float preis, int ve) {
		try {
			out.writeObject("legeMassengutAn");
			out.writeObject(titel);
			out.writeObject(beschreibung);
			out.writeObject(nummer);
			out.writeObject(bestand);
			out.writeObject(preis);
			out.writeObject(ve);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean legeMitarbeiterAn(String mName, String mPasswort) {
		try {
			out.writeObject("legeMitarbeiterAn");
			out.writeObject(mName);
			out.writeObject(mPasswort);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean legeKundenAn(String kName, String kVorname, String kPasswort, String kStrasse, String kHausnummer, int kPlz, String kWohnort) {
		try {
			out.writeObject("legeKundenAn");
			out.writeObject(kName);
			out.writeObject(kVorname);
			out.writeObject(kPasswort);
			out.writeObject(kStrasse);
			out.writeObject(kHausnummer);
			out.writeObject(kPlz);
			out.writeObject(kWohnort);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public int mitarbeiterLogin(String mName, String mPasswort) {					
		try {
			out.writeObject("mitarbeiterLogin");
			out.writeObject(mName);
			out.writeObject(mPasswort);
			int mNr = (Integer) in.readObject();
			return mNr;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public int kundenLogin(String kName, String kPasswort) {					
		try {
			out.writeObject("kundenLogin");
			out.writeObject(kName);
			out.writeObject(kPasswort);
			int kNr = (Integer) in.readObject();
			return kNr;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public Ereignisliste gibAlleEreignisse() {
		try {
			out.writeObject("gibAlleEreignisse");
			Ereignisliste liste = (Ereignisliste) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public KundenListe gibAlleKunden() {
		try {
			out.writeObject("gibAlleKunden");
			KundenListe liste = (KundenListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public MitarbeiterListe gibAlleMitarbeiter() {
		try {
			out.writeObject("gibAlleMitarbeiter");
			MitarbeiterListe liste = (MitarbeiterListe) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Warenkorb gibWarenkorb(int kNr) {
		try {
			out.writeObject("gibWarenkorb");
			out.writeObject(kNr);
			Warenkorb liste = (Warenkorb) in.readObject();
			return liste;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void schreibeArtikel() throws IOException {
		out.writeObject("schreibeArtikel");
	}
	
	public void schreibeKunden() throws IOException {
		out.writeObject("schreibeKunden");
	}
	
	public void schreibeMitarbeiter() throws IOException {
		out.writeObject("schreibeMitarbeiter");
	}
	
	public void schreibeEreignis() throws IOException {
		out.writeObject("schreibeEreignis");
	}

	public boolean inWarenkorbLegen(int kNr, int artNr, int bestand) {
		try {
			out.writeObject("inWarenkorbLegen");
			out.writeObject(kNr);
			out.writeObject(artNr);
			out.writeObject(bestand);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}	
	
	public void warenkorbLoeschen(int kNr) {
		try {
			out.writeObject("warenkorbLoeschen");
			out.writeObject(kNr);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
		
	public Object rechnungDrucken(int kNr){
		try {
			out.writeObject("rechnungDrucken");
			out.writeObject(kNr);
			Object rechnung = in.readObject();
			return rechnung;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
		
	public void artikelKaufen(Warenkorb liste, int kNr) {
		try {
			out.writeObject("artikelKaufen");
			out.writeObject(kNr);
			out.writeObject(liste);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int[] gibHistorie(int artNr, int anzahlTage) {
		try {
			out.writeObject("gibHistorie");
			out.writeObject(artNr);
			out.writeObject(anzahlTage);
			int[] array = (int[]) in.readObject();
			return array;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean artikelLoeschen(int artNr) {
		try {
			out.writeObject("artikelLoeschen");
			out.writeObject(artNr);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean artikelBestand(int artNr, int bestand) {
		try {
			out.writeObject("inWarenkorbLegen");
			out.writeObject(artNr);
			out.writeObject(bestand);
			boolean ok = (Boolean) in.readObject();
			return ok;
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static void main(String args[])
	{
		

        try {
            ShopClient client = new ShopClient();
			MainWindow main = new MainWindow(client);
                
        } catch (IOException e) {
                
                e.printStackTrace();
        }

	}
}

